package com.mdb.open.trade.service.trade;


/*
* Copyright (C), 2017-2019 澳发科技(MDB)
* FileName: TfTradeFileItemInfoService.java
* Author:   you.yichao
* Date:     2019-10-11
* History:
* <author>          <time>          <version>          <desc>
* you.yichao         2019-10-11          V1.00             代码生成器初始生成代码
* -----------------------------------------------------------
* 作者姓名           修改时间           版本号            描述
*/


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mdb.bc.api.data.ListResultEx;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.cache.sequence.util.SequenceGenerator;
import com.mdb.bc.database.mybatisplus.SuperService;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.open.common.enums.TfSequenceEnum;
import com.mdb.open.trade.common.MdbDataSource;
import com.mdb.open.trade.domain.bo.trade.SaveTradeFileItemInfoBo;
import com.mdb.open.trade.domain.bo.trade.UpdateTradeFileItemInfoBo;
import com.mdb.open.trade.domain.po.trade.TradeFileItemInfo;
import com.mdb.open.trade.mapper.trade.TfTradeFileItemInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 类描述：
 *  融资档案商品信息表 服务类
 *
 * @author you.yichao
 * @date 2019-10-11
 */
@Service
@DS(MdbDataSource.TRADE)
public class TfTradeFileItemInfoService extends SuperService<TfTradeFileItemInfoMapper, TradeFileItemInfo> {

    @Autowired
    SequenceGenerator sequenceGenerator;

    public ListResultEx<TradeFileItemInfo> queryItemListByNo(String tradeFileNo) {
        return ListResultEx.ok(this.baseMapper.selectList(new LambdaQueryWrapper<TradeFileItemInfo>()
                .eq(TradeFileItemInfo::getTradeFileNo, tradeFileNo)
                .eq(TradeFileItemInfo::isDeleted, false)));
    }

    public ResultEx saveItemInfo(SaveTradeFileItemInfoBo bo) {
        TradeFileItemInfo saveItemInfo = new TradeFileItemInfo();
        saveItemInfo.setTradeFileNo(bo.getTradeFileNo());
        saveItemInfo.setCustomsNo(bo.getCustomsNo());
        saveItemInfo.setTradeFileCustomsNo(bo.getTradeFileCustomsNo());
        saveItemInfo.setTradeFileItemNo(sequenceGenerator.generator(TfSequenceEnum.TRADE_FILE_ITEM_NO));
        saveItemInfo.setCustomerCode(bo.getCustomerCode());
        saveItemInfo.setMcustomerCode(bo.getMcustomerCode());
        saveItemInfo.setCustomerName(bo.getCustomerName());
        saveItemInfo.setMerchantNo(bo.getMerchantNo());
        saveItemInfo.setMerchantName(bo.getMerchantName());
        saveItemInfo.setBusinessNo(bo.getBusinessNo());
        saveItemInfo.setItemCnName(bo.getItemCnName());
        saveItemInfo.setItemEnName(bo.getItemEnName());
        saveItemInfo.setHsCode(bo.getHsCode());
        saveItemInfo.setBrand(bo.getBrand());
        saveItemInfo.setUnit(bo.getUnit());
        saveItemInfo.setQuantity(bo.getQuantity());
        saveItemInfo.setContractAmount(bo.getContractAmount());
        saveItemInfo.setContractCcy(bo.getContractCcy());
        saveItemInfo.setCustomUnitAmount(bo.getCustomUnitAmount());
        saveItemInfo.setCustomUnitCcy(bo.getCustomUnitCcy());
        saveItemInfo.setCustomTotalAmount(bo.getCustomTotalAmount());
        saveItemInfo.setCustomTotalCcy(bo.getCustomTotalCcy());
        saveItemInfo.setSourceType(bo.getSourceType());

        int count = this.baseMapper.insert(saveItemInfo);
        if (count != 1) {
            return ResultEx.fail();
        }
        return ResultEx.ok();
    }

    public ResultEx updateItemInfo(UpdateTradeFileItemInfoBo bo) {

        TradeFileItemInfo updateItemInfo = new TradeFileItemInfo();
        updateItemInfo.setCustomsNo(bo.getCustomsNo());
        updateItemInfo.setItemCnName(bo.getItemCnName());
        updateItemInfo.setItemEnName(bo.getItemEnName());
        updateItemInfo.setHsCode(bo.getHsCode());
        updateItemInfo.setBrand(bo.getBrand());
        updateItemInfo.setUnit(bo.getUnit());
        updateItemInfo.setQuantity(bo.getQuantity());
        updateItemInfo.setContractAmount(bo.getContractAmount());
        updateItemInfo.setContractCcy(bo.getContractCcy());
        updateItemInfo.setCustomUnitAmount(bo.getCustomUnitAmount());
        updateItemInfo.setCustomUnitCcy(bo.getCustomUnitCcy());
        updateItemInfo.setCustomTotalAmount(bo.getCustomTotalAmount());
        updateItemInfo.setCustomTotalCcy(bo.getCustomTotalCcy());

        LambdaUpdateWrapper<TradeFileItemInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(TradeFileItemInfo::getTradeFileNo, bo.getTradeFileNo());
        if (ObjectUtil.isNotEmpty(bo.getTradeFileItemNo())) {
            updateWrapper.eq(TradeFileItemInfo::getTradeFileItemNo, bo.getTradeFileItemNo());
        }
        if (ObjectUtil.isNotEmpty(bo.getTradeFileCustomsNo())) {
            updateWrapper.eq(TradeFileItemInfo::getTradeFileCustomsNo, bo.getTradeFileCustomsNo());
        }

        int count = this.baseMapper.update(updateItemInfo, updateWrapper);
        if (count != 1) {
            return ResultEx.fail();
        }

        return ResultEx.ok();
    }

    public ResultEx removeItemInfo(UpdateTradeFileItemInfoBo bo) {

        LambdaUpdateWrapper<TradeFileItemInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(TradeFileItemInfo::getTradeFileNo, bo.getTradeFileNo());
        updateWrapper.eq(TradeFileItemInfo::getTradeFileItemNo, bo.getTradeFileItemNo());
        int count = this.baseMapper.delete(updateWrapper);
        if (count != 1) {
            return ResultEx.fail();
        }

        return ResultEx.ok();
    }

}
